import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'


const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function(location) {
  return originalPush.call(this, location).catch(err => {})
};
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'/Home'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta:{
      activeNum:0,
      isShowTabbar:true,
      showFooter:true
    },
    // children: [
    //   {
    //     path: '/searchPopup',
    //     component: resolve => require(['../views/SearchPopup.vue'], resolve)
    //   },
    //   ]
    children:[
      {
        path: '/home/searchPopup',
        name: 'SearchPopup',
        meta:{
          showFooter:false
        },
        component: () => import(/* webpackChunkName: "about" */ '../views/SearchPopup.vue')
      },
    ]
  },
  {
    path: '/topic',
    name: 'Topic',
    meta:{
      activeNum:1,
      isShowTabbar:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/Topic.vue')
  },
  {
    path: '/category',
    name: 'Category',
    meta:{
      activeNum:2,
      isShowTabbar:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/Category.vue')
  },
  {
    path: '/cart',
    name: 'Cart',
    meta:{
      activeNum:3,
      isShowTabbar:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/Cart.vue')
  },
  {
    path: '/user',
    name: 'User',
    meta:{
      activeNum:4,
      isShowTabbar:true
    },
    component: () => import(/* webpackChunkName: "about" */ '../views/User.vue')
  },
  {
    path: '/productDetail',
    name: 'ProductDetail',
    component: () => import(/* webpackChunkName: "about" */ '../views/ProductDetail.vue')
  },

  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  mode:'history',
  base:process.env.BASE_URL,
  routes
})

// 路由拦截，前置路由守卫
router.beforeEach((to,from,next)=>{
  let token=localStorage.getItem("token")
  if(to.path=='/cart'){
    // 判断用户是否登录，判断token是否存在
    if(token){
      next()
    }else {
      Vue.prototype.$toast("请先的登录")
      setTimeout(()=>{
        // 跳转到用户页面
        next("/user")
      },1000)
    }
    return
  }
  // 顺利通过，如果没写这个，则不通过
   next()
})



export default router
